package org.jradweb.tools.generator.xml;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.maven.plugin.logging.Log;
import org.jradweb.tools.generator.freemarker.Value;
import org.jradweb.tools.generator.utils.LogImpl;
import org.jradweb.tools.generator.utils.StringUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.SAXException;

public class SpringControllerConfigOperator extends XmlOperator {

	public SpringControllerConfigOperator(File rootPath, String groupId,
			String artifactId, Log log) throws IOException,
			ParserConfigurationException, SAXException {
		super(rootPath, groupId, artifactId, log);
		File xmlFile = new File(rootPath.getAbsoluteFile()
				+ "/src/main/resources/spring/applicationContext-controller.xml");
		if (!xmlFile.exists()) {
			String msg = "Spring config file not exits, please create it manually: "
					+ xmlFile.getAbsolutePath();
			this.getLog().error(msg);
			throw new IOException(msg);

		}
		this.setXmlFile(xmlFile);
		this.document = this.initDocument(this.getXmlFile());
	}

	public void addContrllerBean(String controllerName, String serviceName,String modelName)
			throws Exception {
		Value controller = new Value(controllerName);
		Value service=new Value(serviceName);
		Value model=new Value(modelName);

		Element beans = document.getDocumentElement();
		Element newBean = document.createElement("bean");
		beans.appendChild(newBean);

		// check if the bean already exists.
		String beanId =controller.getLowerCaseFirst() + "Controller";
		if (this.getBeanById(beanId) != null) {
			this.getLog().info(
					"Spring controller bean \"" + beanId + "\" already exists!");
			return;
		}

		/*------------------------------------------------*/
		/* add new bean attribute (id and class) */
		/*------------------------------------------------*/
		NamedNodeMap nnm = newBean.getAttributes();

		// add id
		Attr id = document.createAttribute("id");
		id.setNodeValue(beanId);
		nnm.setNamedItem(id);

		// add class
		Attr clazz = document.createAttribute("class");
		clazz.setNodeValue(this.groupId + "." + this.artifactId
				+ ".controller." + controller + "Controller");
		nnm.setNamedItem(clazz);

		/*------------------------------------------------*/
		/* Add new bean property */
		/*------------------------------------------------*/
		
		Element propBean1 = document.createElement("property");
		Element valueBean = document.createElement("value");
		propBean1.setAttribute("name", "name");
		propBean1.setAttribute("value", controller.getName());
		newBean.appendChild(propBean1);
		
		 
			
			Element propBean = document.createElement("property");
			Element refBean = document.createElement("ref");

			// Attr propNameAttr = document.createAttribute("name");
			// propNameAttr.setNodeValue("sessionFactory");
			//		
			// Attr refBeanAttr = document.createAttribute("bean");
			// refBeanAttr.setNodeValue("sessionFactory");

			propBean.setAttribute("name", service.getLowerCaseFirst()+"Service");
			refBean.setAttribute("bean", this.groupId+"."+this.artifactId+".service."+service+"Service");

			newBean.appendChild(propBean);
			propBean.appendChild(refBean);
		 

		/*------------------------------------------------*/
		/* Save Document */
		/*------------------------------------------------*/
		this.saveDocument(XML_TYPE_SPRING);
		this.getLog().info("Spring dao bean \"" + beanId + "\" added!");
	}

	public static void main(String[] args) {
		SpringControllerConfigOperator opr;
		String name="customer";
		try {
			File projectRootPath = new File("D:/workspace2/jradtest");
			opr = new SpringControllerConfigOperator(projectRootPath,
					"org.jradweb", "jradtest", new LogImpl(
							SpringControllerConfigOperator.class));
			opr.addContrllerBean(name, name,name);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
